/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.estado.cuentas;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.mybeans.Sesion;
import org.sirmupadbapi.generales.*;
import java.sql.SQLException;

/**
 *
 * @author avbravo
 */
public class EstadoComercio extends javax.swing.JFrame {
    DefaultTableModel tabla = new DefaultTableModel();
    Object[] filas = new Object[10];
    Sesion sesion = new Sesion();
    /**
     * Creates new form EstadoComercio
     */
    public EstadoComercio() {
        initComponents();
        tabla=(DefaultTableModel) this.jTableEstadoComercio.getModel();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jButtonBuscar = new javax.swing.JButton();
        jButtonNueva = new javax.swing.JButton();
        jButtonImprimir = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldNombreComercio = new javax.swing.JTextField();
        jTextFieldID = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableEstadoComercio = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(25, 18, 12));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(47, 151, 18));
        jLabel1.setText("CODIGO DEL COMERCIO");

        jButtonBuscar.setText("Buscar");
        jButtonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscarActionPerformed(evt);
            }
        });

        jButtonNueva.setText("Nueva");
        jButtonNueva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNuevaActionPerformed(evt);
            }
        });

        jButtonImprimir.setText("IMPRIMIR");

        jLabel2.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(47, 151, 18));
        jLabel2.setText("COMERCIO");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldID, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButtonBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jTextFieldNombreComercio)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonNueva, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jButtonBuscar)
                    .addComponent(jButtonNueva)
                    .addComponent(jButtonImprimir)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldNombreComercio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jTableEstadoComercio.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "RECIBO", "CODIGO REGIMEN", "REGIMEN", "FECHA", "VALOR", "SALDO ANTERIOR", "PAGO", "SALDO", "ESTATUS", "TIPO DE PAGO"
            }
        ));
        jScrollPane2.setViewportView(jTableEstadoComercio);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 1168, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 396, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscarActionPerformed
        // TODO add your handling code here:
        try {
            String buscacomercio, buscadatos;
            String idcomercio;
            PreparedStatement consulta1, consulta2;
            ResultSet resul1, resul2;
            int cont, cont2;
            
            //inicio para buscar el nombre del comercio por codigo
           idcomercio=this.jTextFieldID.getText();
           buscacomercio="SELECT comercio from comercios where idcomercio='" + idcomercio + "'";
           sesion.Conectar();
            consulta1=sesion.con.prepareStatement(buscacomercio,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            resul1=consulta1.executeQuery(buscacomercio);
            //este codigo ejecuta el recorrido del resultset para despues obtener la cantidad de filas
            resul1.next();
           
            cont=resul1.getRow();
            if (cont==0){
            Mensajeria.Mensaje("No Existe Un Comercio Registrado Con Este Codigo");
            this.jTextFieldID.selectAll();
            this.jTextFieldID.requestFocus();
            
            }else{
            this.jTextFieldNombreComercio.setText(resul1.getString("comercio"));
            buscadatos = "SELECT recibes.id,recibesdetalles.idrecibo, recibesdetalles.idregimen,regimen.regimen,recibes.fecha, recibesdetalles.valor,recibesdetalles.saldoanterior, recibesdetalles.pago,recibesdetalles.saldo,recibesdetalles.estatus, recibes.tiporecibo  FROM recibesdetalles, recibes, regimen where recibes.id = '"+ idcomercio +"' and recibesdetalles.idregimen=regimen.idregimen and recibesdetalles.idrecibo=recibes.idrecibo and recibes.tipoid='comercios'";
            consulta2= sesion.con.prepareStatement(buscadatos, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            resul2=consulta2.executeQuery(buscadatos);
            resul2.next();
            
            cont2=resul2.getRow();
            
            if (cont2==0){
                Mensajeria.Mensaje("Este Comercio No Tiene Pagos Registrados");
                
            }else{
                resul2.first();
                while(resul2.next()){
                filas[0]=resul2.getInt("recibesdetalles.idrecibo");
                    filas[1]=resul2.getString("recibesdetalles.idregimen");
                    filas[2]=resul2.getString("regimen.regimen");
                    filas[3]=resul2.getString("recibes.fecha");
                    filas[4]=resul2.getDouble("recibesdetalles.valor");
                    filas[5]=resul2.getDouble("recibesdetalles.saldoanterior");
                    filas[6]=resul2.getDouble("recibesdetalles.pago");
                    filas[7]=resul2.getDouble("recibesdetalles.saldo");
                    filas[8]=resul2.getString("recibesdetalles.estatus");
                    filas[9]=resul2.getString("recibes.tiporecibo");
                    
                    tabla.addRow(filas);
                
                }
                
                }
                                 
           }
            
              sesion.con.close();         
        }catch (Exception ex){
            
            Mensajeria.MensajeError(ex, "Error Al Realizar La Operacion" + ex);
            
        }
        
        
             
    }//GEN-LAST:event_jButtonBuscarActionPerformed

    private void jButtonNuevaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNuevaActionPerformed
        // TODO add your handling code here:
         try {
            if (tabla != null) {
                while (tabla.getRowCount() > 0) {
                    tabla.removeRow(0);
                }
            }
            this.jTextFieldNombreComercio.setText("");
            this.jTextFieldID.selectAll();
            this.jTextFieldID.requestFocus();
            
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "LimpiarTabla()");
        }

    }//GEN-LAST:event_jButtonNuevaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EstadoComercio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EstadoComercio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EstadoComercio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EstadoComercio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EstadoComercio().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonBuscar;
    private javax.swing.JButton jButtonImprimir;
    private javax.swing.JButton jButtonNueva;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTableEstadoComercio;
    private javax.swing.JTextField jTextFieldID;
    private javax.swing.JTextField jTextFieldNombreComercio;
    // End of variables declaration//GEN-END:variables
}
